// Notifikasi
var cm_config = {
    home_page: "http://nandarious.blogspot.com",
    max_result: 7,
    t_w: 32,
    t_h: 32,
    new_tab_link: true,
    tt_id: "comments-status",
    ct_id: "status-container",
    new_cm: " Notifikasi",
    interval: 30000,
    alert: true,
    alert: function(total, label) {
         document.getElementById('show-notifikasi').innerHTML = '<strong>'+total+' '+label+'</strong>';
    }
};

function showRecentComments(json) {

	var entry = json.feed.entry,
		total = json.feed.openSearch$totalResults.$t,
		skeleton = "",
		oldCount = parseInt(document.getElementById(cm_config.tt_id).innerHTML,10);
if (oldCount < total) {
		if (cm_config.alert === true) {
			alert((total-oldCount)+cm_config.new_cm);
		} else if (cm_config.alert === false) {
			document.title = '('+(total-oldCount)+cm_config.new_cm+') '+document.title;
		} else {
			cm_config.alert((total-oldCount),cm_config.new_cm);
    }
}
 
	skeleton = '<ul id="status-forum">';
	for (var i = 0; i < entry.length; i++) {
		for (var j = 0; j < entry[i].link.length; j++) {
			if (entry[i].link[j].rel == 'alternate') {
				link = entry[i].link[j].href;
				break;
			}
		}
		var dash = link.lastIndexOf('/')+1,
			dot = link.lastIndexOf('.'),
			title = link.split('-').join(" ").substring(dash,dot);
			author = entry[i].author[0],
			name = author.name.$t,
			avatar = author.gd$image.src.replace(/\/s[0-9]+(\-c|\/)/,"/s"+cm_config.t_w+"$1").replace(/http\:\/\/www.google.com\/url\?source\=imglanding(.*?)q\=/i,"").replace(/\.(jpg|jpeg|png|bmp|gif)(.*?)$/i,".$1"),
			profile = (author.uri) ? author.uri.$t : "#nope",
			date = entry[i].gd$extendedProperty[1].value,
			content = ("content" in entry[i]) ? entry[i].content.$t.replace(/<(.*?)>/g, "") : "",
			nt = (cm_config.new_tab_link) ? ' target="_blank"' : '';
		skeleton += '<li>';
		skeleton += '<a href="'+link+'">'+name+'</a> menulis komentar di '+title+'.';
		skeleton += '</li>';
	}
	skeleton += '</ul>';
	document.getElementById(cm_config.ct_id).innerHTML = skeleton;
	document.getElementById(cm_config.tt_id).innerHTML = total;
}

(function() {
	var head = document.getElementsByTagName('head')[0],
		script = document.createElement('script');
		script.type = "text/javascript";
		script.id = "cm-feed-script";
		script.src = cm_config.home_page+"/feeds/comments/default?alt=json-in-script&redirect=false&max-results="+cm_config.max_result+"&callback=showRecentComments";
	head.appendChild(script);
	setInterval(function() {
		var newScript = document.createElement('script');
			newScript.type = "text/javascript";
			newScript.id = "cm-feed-script";
			newScript.src = cm_config.home_page+"/feeds/comments/default?alt=json-in-script&redirect=false&max-results="+cm_config.max_result+"&callback=showRecentComments";
		var oldScript = document.getElementById('cm-feed-script');
			oldScript.parentNode.removeChild(oldScript);
		head.appendChild(newScript);
	}, cm_config.interval);
})();


